package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.manor.FishSkillData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @date ：2021/10/14 14:39
 */
public class UserFishPondFishSkillDataDaoImpl extends TempDataObjectDao<FishSkillData> {
    private static String insertSql = "insert into t_u_fishpond_fishskill(userId,fishId,fishSkillId,lv,curExp) values(?,?,?,?,?) ";
    @Override
    protected boolean addData(FishSkillData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getFishId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getFishSkillId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLv()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCurExp()));
        return getDb().executeNoneQuery(insertSql, para) > 0;
    }

    private static String updateSql = "update t_u_fishpond_fishskill set lv=?, curExp=? where userId=? and fishId=? and fishSkillId=?";
    @Override
    protected boolean updateData(FishSkillData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getLv()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCurExp()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getFishId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getFishSkillId()));
        return getDb().executeNoneQuery(updateSql, para) > 0;
    }

    @Override
    protected FishSkillData parseData(ResultSet rs) throws SQLException {
        FishSkillData fishSkillData = new FishSkillData();
        fishSkillData.setUserId(rs.getLong("userId"));
        fishSkillData.setFishId(rs.getInt("fishId"));
        fishSkillData.setFishSkillId(rs.getInt("fishSkillId"));
        fishSkillData.setLv(rs.getInt("lv"));
        fishSkillData.setCurExp(rs.getLong("curExp"));
        return fishSkillData;
    }

    private static String getSql = "select * from t_u_fishpond_fishskill where userId = ? and fishId = ?";

    public Map<Integer, FishSkillData> getFishSkillDataMap(long playerId, int fishId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, FishSkillData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getSql);
            call.setLong(1, playerId);
            call.setInt(2, fishId);
            rs = call.executeQuery();
            while (rs.next()) {
                FishSkillData data = parse(rs);
                map.put(data.getFishSkillId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }
}
